﻿package com.scn.base.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;


import com.scn.base.domain.UserVO;
import com.scn.base.page.PageVO;
import com.scn.base.service.ISysUserService;
import com.scn.base.util.MD5Util;


@Controller
@SessionAttributes("user")
public class SysUserController {
	@Autowired
	private ISysUserService sysUserService;
	private Logger log=Logger.getLogger(SysUserController.class);
	
	/**
	 * 功能描述：添加用户页面
	 * @return 模型视图
	 */
	@RequestMapping(value="/userAddView.html")
	public ModelAndView  userAddView(){
		ModelAndView mav=new ModelAndView("/sysuser/userAdd");
		return mav;
	}
	
	@RequestMapping(value="/userList.html")
	public ModelAndView  userList(){
		ModelAndView mav=new ModelAndView("/sysrole/rolePowerList");
		mav.addObject("userList", sysUserService.selectUserListByRole(0));
		return mav;
	}
	@RequestMapping(value="/sysUserList.html")
	public ModelAndView sysUserList(@ModelAttribute("userinfo")UserVO user,String currPage){
		ModelAndView mav=new ModelAndView("/sysuser/userList");
		if(user==null){
			 mav.addObject("checkmes", "用户名为空！");
			 return mav;
		 }
		int counts=sysUserService.selectUserListCount();
		PageVO page=new PageVO();
		 if(currPage==null||"".equals(currPage)){
			 page.setCurPage(1);
		 }else{
			 page.setCurPage(Integer.parseInt(currPage));
		 }
		page.setRecordCounts(counts);
		List sysUserlist=sysUserService.selectUserList(page);
		//dbLog(user.getUserno(), "SysUserAction->userList", "用户清单查询");
		mav.addObject("sysUserlist", sysUserlist);
		mav.addObject("pagevo", page);
	
		return mav;
	}
	@RequestMapping(value="/insertUser.html")
	public ModelAndView insertUser(String saveflag,String username,String userno,String password,String srpname,String email,String sex,String tel,String desc,String userstatus,String usersite){
		UserVO userVo=new UserVO();
		userVo.setUserid((int)sysUserService.selectUserSeq());
		userVo.setUsername(username);
		userVo.setUserno(userno);
    	userVo.setUserpass(MD5Util.md5Encrypt(password));
		userVo.setUserrole(Integer.parseInt(srpname));
		userVo.setUsermail(email);
		userVo.setUserSex(1);
		userVo.setUsertel(tel);
		userVo.setUserdesc(desc);
		userVo.setUserstatus(Integer.parseInt(userstatus));
		userVo.setUsersite(usersite);
		sysUserService.insertUser(userVo);
		return new ModelAndView("forward:/sysUserList.html");
	}
	
	@RequestMapping(value="/updateUser.html")
	public ModelAndView updateUser(String userId,String srpname,String username,String userdesc){
		
		//用户所属角色
		UserVO userVo =sysUserService.selectUserById(Integer.parseInt(userId));

		Map<String,Object> userMap=new HashMap<String,Object>();
		userMap.put("username_new",username);
		userMap.put("userdesc_new", userdesc);
		userMap.put("userrole_new", Integer.parseInt(srpname));
		
		userMap.put("username_old", userVo.getUsername());
		userMap.put("userdesc_old", userVo.getUserdesc());
		userMap.put("userrole_old", userVo.getUserrole());
		sysUserService.updateUser(userMap);
		return new ModelAndView("forward:/sysUserList.html");
	}
	
	@RequestMapping(value="/initPwd.html")
	public ModelAndView initPwd(String userId){
		UserVO usreVo=sysUserService.selectUserById(Integer.parseInt(userId));
		Map<String,String> initPwdMap=new HashMap<String,String>(2);
		initPwdMap.put("init_userpass", MD5Util.md5Encrypt("123"));
		initPwdMap.put("old_userpass", usreVo.getUserpass());
		sysUserService.initPwd(initPwdMap);
		return new ModelAndView("forward:/sysUserList.html");
	}
	@RequestMapping(value="/deleteUser.html")
	public ModelAndView deleteUser(String userId){
		sysUserService.deleteUserById(Integer.parseInt(userId));
		return new ModelAndView("forward:/sysUserList.html");
	}
	@RequestMapping(value="/preUpdateUser.html")
	public ModelAndView preUpdateUser(String userId){
	    UserVO usreVo=sysUserService.selectUserById(Integer.parseInt(userId));
	    ModelAndView mav=new ModelAndView("/sysuser/userPreUpdate");
	    mav.addObject("usreVo", usreVo);
		return mav;
	}
}
